package ru.jexp.rcp.supply.login;

import org.eclipse.core.runtime.ListenerList;
import org.eclipse.jface.dialogs.IDialogConstants;
import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Shell;

import ru.jexp.rcp.supply.img.JExpImageLink;
import ru.jexp.rcp.supply.mui.JExpSupplyMessages;
import ru.jexp.rcp.supply.swtext.JExpDialog;
import ru.jexp.rcp.supply.swtext.JExpText;
import ru.jexp.rcp.supply.swtext.factory.JExpControlFactory;
import ru.jexp.rcp.supply.swtext.factory.JExpLayoutFactory;
import ru.jexp.utilities.StringUtil;

public class JExpLoginDialog extends JExpDialog implements IJExpLoginDialog {

	private JExpText login;
	private JExpText password;
	private ListenerList listeners = new ListenerList(1);
	private Label why;
	private Combo servers;

	public JExpLoginDialog(Shell parentShell) {
		super(parentShell);
	}

	public void addLoginDialogListener(LoginDialogListener listener) {
		listeners.add(listener);
	}

	@Override
	public void applyLogin() {
		super.okPressed();
	}

	@Override
	public String getLogin() {
		return login.getValue();
	}

	@Override
	public String getPassword() {
		return password.getValue();
	}

	@Override
	public void setAvailableServers(String[] servers) {
		this.servers.removeAll();
		this.servers.setItems(servers);
	}

	@Override
	public void setLogin(String login) {
		this.login.setValue(login);
	}

	@Override
	public void setServer(int server) {
		this.servers.select(server);
	}

	@Override
	public String getServer() {
		return servers.getText();
	}

	@Override
	public void regectLogin(String why) {
		this.why.setText(why);
	}

	@Override
	protected void createControls(Composite parent) {
		parent.setLayout(new GridLayout(2, false));
		parent.setLayoutData(JExpLayoutFactory.gdFillBoth());
		JExpControlFactory.createLabel(parent,
				JExpSupplyMessages.Login_dialog_field_login);
		login = JExpControlFactory.createText(parent);
		login.setLayoutData(JExpLayoutFactory.gdWithSizeHorizontal(150));
		JExpControlFactory.createLabel(parent,
				JExpSupplyMessages.Login_dialog_field_password);
		password = JExpControlFactory.createText(parent, SWT.PASSWORD);
		password.setLayoutData(JExpLayoutFactory.gdWithSizeHorizontal(150));
		JExpControlFactory.createLabel(parent,
				JExpSupplyMessages.Login_dialog_field_server);
		servers = new Combo(parent, SWT.BORDER);
		servers.setLayoutData(JExpLayoutFactory.gdWithSizeHorizontal(135));
		why = JExpControlFactory.createLabel(parent, "", SWT.WRAP);
		why.setLayoutData(JExpLayoutFactory.gdFillHorizontal(2));
		why.setForeground(Display.getCurrent().getSystemColor(SWT.COLOR_RED));
	}
	
	@Override
	protected Image getImageDescription() {
		return JExpImageLink.getImage(JExpImageLink.LOGON);
	}

	@Override
	protected String createTitle() {
		return JExpSupplyMessages.Login_dialog_title;
	}

	@Override
	protected void updateControlsData() {
		fireLoginDialogChanged(LoginDialogListener.UPDATE_DATA);
		if (!StringUtil.isEmptyWithoutSpace(login.getValue())) {
			password.setFocus();
		}
	}

	@Override
	protected void updateControlsEnable() {
	}

	@Override
	protected Button createButton(Composite parent, int id, String label,
			boolean defaultButton) {
		Button button = super.createButton(parent, id, label, defaultButton);
		if (IDialogConstants.OK_ID == id) {
			button.setText(JExpSupplyMessages.Login_dialog_button_enter);
		}
		return button;
	}

	@Override
	protected void okPressed() {
		fireLoginDialogChanged(LoginDialogListener.LOGON);
	}

	private GridData visibleInvisible(boolean invisible) {
		GridData gd = new GridData(SWT.FILL, SWT.NONE, true, true);
		gd.horizontalSpan = 2;
		if (invisible) {
			gd.heightHint = 0;
		}
		return gd;
	}

	private void fireLoginDialogChanged(int event) {
		Object[] copyListeners = listeners.getListeners();
		for (int i = 0; i < copyListeners.length; i++) {
			LoginDialogListener logonListener = (LoginDialogListener) copyListeners[i];
			logonListener.event(event);
		}
	}
}
